package com.example.weatherapp233;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import com.example.weatherapp233.adapter.GuidePagerAdapter;

public class GuideActivity extends AppCompatActivity {
    private ViewPager2 viewPager;
    private Button btnStart;
    private SharedPreferences sharedPreferences;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        
        sharedPreferences = getSharedPreferences("app_prefs", MODE_PRIVATE);
        boolean isFirstLaunch = sharedPreferences.getBoolean("is_first_launch", true);
        
        if (!isFirstLaunch) {
            startMainActivity();
            return;
        }
        
        viewPager = findViewById(R.id.viewPager);
        btnStart = findViewById(R.id.btnStart);
        
        GuidePagerAdapter adapter = new GuidePagerAdapter(this);
        viewPager.setAdapter(adapter);
        
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                btnStart.setVisibility(position == adapter.getItemCount() - 1 ? View.VISIBLE : View.GONE);
            }
        });
        
        btnStart.setOnClickListener(v -> {
            sharedPreferences.edit().putBoolean("is_first_launch", false).apply();
            startMainActivity();
        });
    }
    
    private void startMainActivity() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }
}